package SymbolTable;

import java.util.*;

import antlr.collections.AST;

import JavaGrammar.JavaTokenTypes;
import util.Util;

//////////////////////////////////////////////////////////////////////////
//// STLocalVariableEntry
/**

@author Ivan Jeukens
@version v 0.1 03/10/2001
*/
public class STLocalVariableEntry extends STEntry {

    public STLocalVariableEntry(String name, int mode, AST node, 
            int narray) {
        super(name, mode, node, narray);
    }

    ///////////////////////////////////////////////////////////////////
    ////                        public methods                     ////

    /* Data uma expressao de definicao de variavel, constroi a respectiva 
    entrada local de variavel. O nome do tipo tem que estar definido */
    public static STLocalVariableEntry generateSTEntry(AST vardef, String className,
            String pkgName, LinkedList imports) {                                        
        if(vardef.getType() != JavaTokenTypes.VARIABLE_DEF) return null;

        // modifier
        AST v = vardef.getFirstChild();

        // typespec
        v = v.getNextSibling();
        AST tast = v.getFirstChild();
        int arraycount = 0;
        if(tast.getType() == JavaTokenTypes.ARRAY_DECLARATOR) {
            arraycount = 1;
            while(tast.getType() == JavaTokenTypes.ARRAY_DECLARATOR) {
                tast = tast.getFirstChild();
                arraycount++;
            }            
        }

        String typename = null;
        switch(tast.getType()) {
            case JavaTokenTypes.IDENT: {
                typename = tast.getText();
            } break;
            
            case JavaTokenTypes.LITERAL_void: {
                typename = "void";
            } break;
            
            case JavaTokenTypes.LITERAL_boolean: {
                typename = "boolean";
            } break;
            
            case JavaTokenTypes.LITERAL_byte: {
                typename = "byte";
            } break;
            
            case JavaTokenTypes.LITERAL_char: {
                typename = "char";
            } break;
            
            case JavaTokenTypes.LITERAL_short: {
                typename = "short";
            } break;
            
            case JavaTokenTypes.LITERAL_int: {
                typename = "int";
            } break;
            
            case JavaTokenTypes.LITERAL_float: {
                typename = "float";
            } break;
            
            case JavaTokenTypes.LITERAL_long: {
                typename = "long";
            } break;
            
            case JavaTokenTypes.LITERAL_double: {
                typename = "double";
            } break;
            
            default: typename = null;
        }

        // declarator
        v = v.getNextSibling();
        String varName = v.getText();

        STLocalVariableEntry entry = new STLocalVariableEntry(varName, STEntry.LOCAL,
	    vardef, arraycount);

        Class type = Util.getType(typename, className, pkgName, imports);
        entry.setType(type);
            
        return entry;
    }
    
    public boolean isPrimitive() {
        return _isPrimitive;
    }

    public void setPrimitive(boolean value) {
        _isPrimitive = value; 
    }
    
    public void setStatementIndex(int value) {
        _statementIndex = value;
    }
    
    public int getStatementIndex() {
        return _statementIndex;
    }

    public String toString() {
        return new String("### Local variable " + getName() + 
            " isPrimitive? = " + _isPrimitive + " Statement index = " +
            _statementIndex + " nArray= " + _narray + " type = " +
            _type);
    }

    ///////////////////////////////////////////////////////////////////
    ////                        private variables                  ////

    private boolean _isPrimitive;
    private int _statementIndex;
}
